home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 May / CMCD0504.ISO / Software / Freeware / Programare / gdiplusdelphi / demos / Constructing and Drawing Paths / Creating Figures from Lines, Curves, and Shapes / GDITEST5.dpr
Encoding:
Text File  |  2003-10-15  |  4.5 KB  |  150 lines

  1. program GDITEST5;
  2.  
  3. uses
  4.   Windows,
  5.   Messages,
  6.   SysUtils,
  7.   GDIPAPI,
  8.   GDIPOBJ;
  9.  
  10. Procedure OnPaint(DC: HDC);
  11. var
  12.   graphics : TGPGraphics;
  13.   Pen : TGPPen;
  14.   path : TGPGraphicsPath;
  15.   brush: TGPSolidBrush;
  16. const
  17.   points : array[0..2] of TGPPoint =
  18.     ((x: 40; y: 60),
  19.      (x: 50; y: 70),
  20.      (x: 30; y: 90));
  21. begin
  22.   graphics := TGPGraphics.Create(DC);
  23.  
  24.   // To create a path, construct a GraphicsPath object, and then call methods,
  25.   // such as AddLine and AddCurve, to add primitives to the path.
  26.  
  27.   // The following example creates a path that has a single arc. The arc has a
  28.   // sweep angle of รป180 degrees, which is counterclockwise in the default
  29.   // coordinate system.
  30.  
  31.     Pen := TGPPen.Create(MakeColor(255, 255, 0, 0));
  32.     path := TGPGraphicsPath.Create;
  33.     path.AddArc(175, 50, 50, 50, 0, -180);
  34.     graphics.DrawPath(pen, path);
  35.  
  36.   // The following example creates a path that has two figures. The first figure
  37.   // is an arc followed by a line. The second figure is a line followed by a
  38.   // curve, followed by a line. The first figure is left open, and the second
  39.   // figure is closed.
  40.  
  41.     Pen.SetWidth(2);
  42.  
  43.     // The first figure is started automatically, so there is
  44.     // no need to call StartFigure here.
  45.     path.Reset;
  46.     path.AddArc(175, 50, 50, 50, 0.0, -180.0);
  47.     path.AddLine(100, 0, 250, 20);
  48.  
  49.     path.StartFigure();
  50.     path.AddLine(50, 20, 5, 90);
  51.     path.AddCurve(PGPPoint(@points), 3);
  52.     path.AddLine(50, 150, 150, 180);
  53.     path.CloseFigure();
  54.  
  55.     graphics.DrawPath(pen, path);
  56.  
  57.   // In addition to adding lines and curves to paths, you can add closed shapes:
  58.   // rectangles, ellipses, pies, and polygons. The following example creates a
  59.   // path that has two lines, a rectangle, and an ellipse. The code uses a pen
  60.   // to draw the path and a brush to fill the path.
  61.  
  62.     brush := TGPSolidBrush.Create(MakeColor(255, 0, 0, 200));
  63.     path.Reset;
  64.     path.AddLine(210, 210, 300, 240);
  65.     path.AddLine(300, 260, 230, 260);
  66.     path.AddRectangle(MakeRect(250, 235, 20, 40));
  67.     path.AddEllipse(210, 275, 40, 30);
  68.  
  69.     graphics.DrawPath(pen, path);
  70.     graphics.FillPath(brush, path);
  71.  
  72.   // The path in the preceding example has three figures. The first figure
  73.   // consists of the two lines, the second figure consists of the rectangle,
  74.   // and the third figure consists of the ellipse. Even when there are no calls
  75.   // to CloseFigure or StartFigure, intrinsically closed shapes, such as
  76.   // rectangles and ellipses, are considered separate figures.
  77.  
  78.   Pen.Free;
  79.   path.Free;
  80.   brush.Free;
  81.   graphics.Free;
  82. end;
  83.  
  84.  
  85. function WndProc(Wnd : HWND; message : UINT; wParam : Integer; lParam: Integer) : Integer; stdcall;
  86. var
  87.   Handle: HDC;
  88.   ps: PAINTSTRUCT;
  89. begin
  90.   case message of
  91.     WM_PAINT:
  92.       begin
  93.         Handle := BeginPaint(Wnd, ps);
  94.         OnPaint(Handle);
  95.         EndPaint(Wnd, ps);
  96.         result := 0;
  97.       end;
  98.  
  99.     WM_DESTROY:
  100.       begin
  101.         PostQuitMessage(0);
  102.         result := 0;
  103.       end;
  104.  
  105.    else
  106.       result := DefWindowProc(Wnd, message, wParam, lParam);
  107.    end;
  108. end;
  109.  
  110. var
  111.   hWnd     : THandle;
  112.   Msg      : TMsg;
  113.   wndClass : TWndClass;
  114. begin
  115.    wndClass.style          := CS_HREDRAW or CS_VREDRAW;
  116.    wndClass.lpfnWndProc    := @WndProc;
  117.    wndClass.cbClsExtra     := 0;
  118.    wndClass.cbWndExtra     := 0;
  119.    wndClass.hInstance      := hInstance;
  120.    wndClass.hIcon          := LoadIcon(0, IDI_APPLICATION);
  121.    wndClass.hCursor        := LoadCursor(0, IDC_ARROW);
  122.    wndClass.hbrBackground  := HBRUSH(GetStockObject(WHITE_BRUSH));
  123.    wndClass.lpszMenuName   := nil;
  124.    wndClass.lpszClassName  := 'GettingStarted';
  125.  
  126.    RegisterClass(wndClass);
  127.  
  128.    hWnd := CreateWindow(
  129.       'GettingStarted',       // window class name
  130.       'Creating Figures from Lines, Curves, and Shapes',       // window caption
  131.       WS_OVERLAPPEDWINDOW,    // window style
  132.       Integer(CW_USEDEFAULT), // initial x position
  133.       Integer(CW_USEDEFAULT), // initial y position
  134.       Integer(CW_USEDEFAULT), // initial x size
  135.       Integer(CW_USEDEFAULT), // initial y size
  136.       0,                      // parent window handle
  137.       0,                      // window menu handle
  138.       hInstance,              // program instance handle
  139.       nil);                   // creation parameters
  140.  
  141.    ShowWindow(hWnd, SW_SHOW);
  142.    UpdateWindow(hWnd);
  143.  
  144.    while(GetMessage(msg, 0, 0, 0)) do
  145.    begin
  146.       TranslateMessage(msg);
  147.       DispatchMessage(msg);
  148.    end;
  149. end.
  150.